// pages/medicalRecord/medicalRecord.js
import {getMedicalRecord , getIllnessDictionary , patchMedicalRecord} from "../../services/_utils/api"
import { GETSTORAGE } from "../../services/storage/storage"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    autosize: {
      maxHeight: 120,
      minHeight: 20,
    },
    start: '1900-01-01 00:00:00',//出生日期最早时间
    end: '2024',//出生日期最晚时间
    operation:new Date().getTime(), //手术时间
    operationVisible: false,
    operationText: '',
    leftLeg: false, //左腿
    rightLeg: false, //右腿
    treatDoctorValue:'',//主治医生
    hospitalValue:'',//治疗医院
    bedNumberValue:'',//住院床号
    medicalRecordId:'',//病历表id
    data:{},
    cardiopathyText:'无', //心脏病
    cerebrovascularText:'无', //脑血管
    hyperthrombosisText:'无', //高血栓
    cardiopathyList:[],
    cerebrovascularList:[],
    hyperthrombosisList:[],
  },
  submit(){
    const userid = GETSTORAGE('userId')
    const infoid = GETSTORAGE('recoveryMemberInfoId')
    const that = this.data
    let userCase = {
      userId:userid,
      recoveryMemberInfoId:infoid,
      medicalRecordId:that.medicalRecordId, //病历表id
      leftLeg:that.leftLeg == true ? '1' : '0',      //手术左腿   
      rightLeg:that.rightLeg == true ? '1' : '0',    //手术右腿
      treatDoctor:that.treatDoctorValue,    //主治医生
      operativeTime:that.operation,    //手术时间    
      hospital:that.hospitalValue,      //治疗医院     
      heartMedicalHistory: [that.cardiopathyText],     //心脏病
      cerebrovascularMedicalHistory: [that.cerebrovascularText] , //脑血管
      thrombosisMedicalHistory:[that.hyperthrombosisText], //高血栓
      bedNumber:that.bedNumberValue //住院床号
    }
    patchMedicalRecord(userCase).then(res => {
      console.log(res,"修改成功");
      if(res.data.code == 200){
        wx.navigateTo({
          url: '/pages/userCase/userCase',
        })
      }
    })
    console.log(userCase);
  },
  // 左腿切换
  leftLegChange(e){
    const flag = !this.data.leftLeg
    this.setData({
      leftLeg: flag,
      rightLeg:this.data.rightLeg
    })
  },
  // 右腿切换
  rightLegChange(e){
    const flag = !this.data.rightLeg
    this.setData({
      rightLeg: flag,
      leftLeg:this.data.leftLeg
    })
  },
  getIllnessDictionary(){
    getIllnessDictionary().then(res => {
      console.log(res,"病历史");
     res.data.data.allDisease.forEach(item => {
       if(item.medicalHistory == '心脏病史'){
         let obj = {
           value:item.illness,
           label:item.illness
         }
         this.data.cardiopathyList.push(obj)
         this.setData({
          cardiopathyList:this.data.cardiopathyList
         })
       }else if(item.medicalHistory == '脑血管病史'){
        let obj = {
          value:item.illness,
          label:item.illness
        }
        this.data.cerebrovascularList.push(obj)
        this.setData({
          cerebrovascularList:this.data.cerebrovascularList
        })
       }else{
        let obj = {
          value:item.illness,
          label:item.illness
        }
        this.data.hyperthrombosisList.push(obj)
        this.setData({
          hyperthrombosisList:this.data.hyperthrombosisList
        })
       }
     })
    })
  },
  // 心脏病选择器显示
  onCardiopathyPicker(e){
    const { key } = e.currentTarget.dataset;
    console.log(e);
    this.setData({
      [`${key}Visible`]: true
    })
  },
  // 住院床号
  onBedNumberInput(e){
    const {value} = e.detail
    this.setData({
      bedNumberValue : value
    })
  },
  // 心脏病确认
  oncardiopathyChange(e){
    const { key } = e.currentTarget.dataset;
    const { value } = e.detail;
    this.setData({
      [`${key}Visible`]: false,
      [`${key}Value`]: value,
      [`${key}Text`]: value.join(' '),
    });
  },
  // 心脏病取消
  oncardiopathyCancel(e){
    const { key } = e.currentTarget.dataset;
    this.setData({
      [`${key}Visible`]: false
    });
  },
  // 治疗医院
  onHospitalInput(e){
    const {value} = e.detail
    this.setData({
      hospitalValue : value
    })
  },
  // 主治医生
  onTreatDoctorInput(e){
    const {value} = e.detail
    this.setData({
      treatDoctorValue : value
    })
  },
  // 出生年月日弹出框显示
  showPicker(e) {
    const { mode } = e.currentTarget.dataset;
    this.setData({
      mode,
      [`${mode}Visible`]: true,
    });
  },
  // 出生年月日取消
  hidePicker() {
    const { mode } = this.data;
    this.setData({
      [`${mode}Visible`]: false,
    });
  },
   // 出生年月日确定
   onConfirm(e) {
    const { value } = e.detail;
    const { mode } = this.data;
    this.setData({
      [mode]: value,
      [`${mode}Text`]: value,
    });
    this.hidePicker();
  },
  // 获取病历表信息
  getCasehistory(val){
    const id = GETSTORAGE('recoveryMemberInfoId')
    getMedicalRecord({recoveryMemberInfoId:id}).then(res => {
      console.log(res,"病历信息");
      const list = res.data.data
      list.forEach(item => {
        item.createTime = new Date(item.createTime).toLocaleDateString()
        if(item.id == val){
          console.log(item,"id相同");
          this.setData({
            operation:item.operativeTime,
            operationText:item.operativeTime,
            leftLeg:item.leftLeg,
            rightLeg:item.rightLeg,
            treatDoctorValue:item.treatDoctor,
            hospitalValue:item.hospital,
            cardiopathyText:item.heartMedicalHistory[0], //心脏病
            cerebrovascularText:item.cerebrovascularMedicalHistory[0], //脑血管
            hyperthrombosisText:item.thrombosisMedicalHistory[0], //高血栓
            bedNumberValue:item.bedNumber
          })
          console.log(item.heartMedicalHistory);
        }
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options,"options");
    this.getCasehistory(options.ID)
    this.getIllnessDictionary()
    this.setData({
      medicalRecordId : options.ID
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})